package cn.hnucm.service.Impl;

import cn.hncum.feign.clients.UserClient;
import cn.hnucm.bean.TbUserBasicMessage;
import cn.hnucm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:liweihua
 * @Description:    登录逻辑
 * @Date: 11:01$ 2021/12/18/018$
 * @Modified:
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private PasswordEncoder pw;

    @Autowired
    UserClient userClient;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("进入登录逻辑");
        List<TbUserBasicMessage> tbUserBasicMessages = userClient.loginVerify(s);
        if (tbUserBasicMessages.size() == 0 || tbUserBasicMessages== null){
            //查不到或者查询异常
            throw new UsernameNotFoundException("用户名不存在");
        }
        User user = new User();
        user.init(tbUserBasicMessages.get(0));
        user.setUserPassword(pw.encode(user.getUserPassword()));
        return user;
    }
}
